package com.lanou.moving.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lanou.moving.R;
import com.lanou.moving.entity.FindRoot;

import java.util.ArrayList;
import java.util.HashMap;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/23.
 */
public class FindViewPagerAdapter extends PagerAdapter {
    private Context context;
    private ArrayList<FindRoot.DatasEntity.DataEntity> datasEntities;
    private ImageView find_head_ima;

    public FindViewPagerAdapter(Context context, ArrayList<FindRoot.DatasEntity.DataEntity> datasEntities) {
        this.context = context;
        this.datasEntities = datasEntities;
    }

    @Override
    public int getCount() {
        return datasEntities != null && datasEntities.size() > 0 ? datasEntities.size() : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        HashMap<Integer, View> viewMap = new HashMap<Integer, View>();
        View view = null;
        if (viewMap.containsKey(position))
        {
            view = viewMap.get(position);
        }else {
        view = LayoutInflater.from(context).inflate(R.layout.find_viewpager_head_item, null);
            viewMap.put(position, view);
            find_head_ima = (ImageView) view.findViewById(R.id.find_head_ima);
            find_head_ima.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            String picUrl = datasEntities.get(position % datasEntities.size()).getPicUrl();
            if (picUrl != null) {
                Picasso.with(context).load(picUrl).into(find_head_ima);
            }
        }
        container.addView(view);
        return view;
    }
}
